<?php

class Admin_baseController extends Controller {

    public static $admin_info = false;

    public function __construct() {
        parent::__construct();
        $this->checkLogin();
        if(self::$admin_info['role_id'] != admin_roleModel::ROLE_SHOP_ADMIN) {
            $admin_nodel_ids = self::model('admin_access')->getAllByRoleId(self::$admin_info['role_id']);
            $this->checkAccess(self::$admin_info, $admin_nodel_ids);
        }
    }
    
    //设置密码
    public function settingAction() {
        if (Request::isGet()) {
            $this->display();
            exit;
        }
        $data = array();
        $data['password'] = Request::getParams('password');
        $data['newpassword'] = Request::getParams('newpassword');
        if(!$data['password']||!$data['newpassword']) {
            Response::ajax(NORMAL_ERROR, '密码不能为空');
        }
        if(!$admin_info=self::model('admin')->getOne('admin_id=?', array(self::$admin_info['admin_id']))) {
            Response::ajax(NORMAL_ERROR, '管理员账号不已存在,修改失败');
        }
        if($admin_info['password']!=md5($data['password'].$admin_info['salt'])) {
            Response::ajax(NORMAL_ERROR, '当前密码填写有误,修改失败');
        }
        if(!Validation::isLength($data['newpassword'], 6, 15)) {
            Response::ajax(NORMAL_ERROR, '新密码长度在6-15位之间');
        }
        
        $data['salt'] = Text::randString(4);
        $data['password'] = md5($data['newpassword'].$data['salt']);
        if (self::model('admin')->update($data, 'admin_id=?', array($admin_info['admin_id']))) {
            $this->addLog('管理员'. self::$admin_info['name'].'修改管理员密码');
            Response::ajax(200, '更新管理员信息成功');
        }
        Response::ajax(NORMAL_ERROR, '操作失败，请稍后再试');
    }

    //根据管理员权限创建头部菜单
    public function initHeader($admin_info, $access_node_ids) {
        $admin_header = self::model('admin_node')->getHeaderList();
        if($admin_info['role_id']!=1){
            foreach ($admin_header as $key=>$row) {
                if(!in_array($row['id'], $access_node_ids)) {
                    unset($admin_header[$key]);
                }
            }
        }
        $this->assign('adminHeader', $admin_header);
    }

    //根据管理员权限获取相应的菜单列表
    public function initMenu($admin_info, $access_node_ids) {
        $admin_node_show_list = self::model('admin_node')->getCacheList();
        if($admin_info['role_id']!=1){
            foreach ($admin_node_show_list as $key=>$row) {
                if(!in_array($row['id'], $access_node_ids)) {
                    unset($admin_node_show_list[$key]);
                }
            }
        }
        $this->assign('adminMenu', self::model('admin_node')->getTree($admin_node_show_list));
    }

    public function checkLogin($is_must = true) {
        if (self::$admin_info) {
            return true;
        }
        if (self::$admin_info = self::model('admin')->checkLogin()) {
            $this->assign('admin_info', self::$admin_info);
            return true;
        }
        if ($is_must) {
            if (Request::isAjax()) {
                Response::ajax(USER_NO_LOGIN, '抱歉,您暂未登录');
            }
            Response::showMsg('抱歉,您暂未登录', Request::getServerName() . Router::getBaseUrl() . '/admin_index/login');
        }
        return false;
    }

    public function addLog($msg) {
        $data = array(
            'controller' => Doit::getControllerName(),
            'action' => Doit::getActionName(),
            'ip' => Request::getClientIp(),
            'admin_id' => self::$admin_info['admin_id'],
            'msg' => $msg,
            'add_time' => date('Y-m-d H:i:s')
        );
        self::model('admin_log')->insert($data);
        return true;
    }

    public function checkAccess($admin_info, $access_node_ids) {
        if($admin_info['role_id']==1) {
            return true;
        }
        $url_base = Doit::getControllerName().'/'.Doit::getActionName();
        $all_node_list = self::model('admin_node')->getAllCacheList();
        $checkIds = [];
        $check_id = 0;
        foreach ($all_node_list as $row) {
            if($row['is_show']) {
                continue;
            }
            if(strpos($row['url_path'], $url_base)) {
                $checkIds[] = $check_id;
            }
        }
        if($checkIds) {
            foreach ($checkIds as $checkId) {
                if(in_array($checkId, $access_node_ids)) {
                    return true;
                }
            }
            Response::showMsg('抱歉,您暂时没有权限访问该信息,请联系管理员', -1);
            return false;
        }
        return true;
    }

}
